package com.wl.dzt.sys.core.dao.select.mapper;


import com.wl.dzt.sys.util.FieldUtil;

import java.util.Map;

/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description 数据适配
 * 把map转换为实体
 */
public interface DataMapper<T> {

    /**
     * <b>适配</b>
     * <p>
     * 将查询结果和实体适配<br>
     * 配处大概需要消耗5-10倍左右性能,随字段增加而增加（需要装配实体）
     * </p>
     *
     * @param clazz 类
     * @param row   列信息
     * @return 封装好的对象
     * @throws InstantiationException InstantiationException
     * @throws IllegalAccessException IllegalAccessException
     */
    public default Object mapper(Class<T> clazz, Map<String, Object> row) throws InstantiationException, IllegalAccessException {
        Object entity = clazz.newInstance();
        row.forEach((key, val) -> {
            String k = key.replaceAll("_", ".");
            try {
                FieldUtil.setValueByCascade(entity, k, val);
            } catch (IllegalArgumentException | IllegalAccessException | InstantiationException e) {
                e.printStackTrace();
            }
        });
        return entity;
    }

}
